﻿using Aspose.Cells;
using ProjectCloth.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class ImportCompanyFrm : Form
    {
        public ImportCompanyFrm()
        {
            InitializeComponent();
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            label1.Text = "导入商品条数：0";
            //FileDialog fileDialog = new FileDialog();
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "所有文件(*.*)|*.*";
            string file = "";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                file = dialog.FileName;
                var data = ExcelToTable(file);
                label1.Text = "导入商品条数：" + data.ToString();

            }
            MessageBox.Show("窗口将关闭！");
            this.Close();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public int ExcelToTable(string filePath)
        {
            Workbook book = null;
            Worksheet sheet = null;
            book = new Workbook(filePath);
            sheet = book.Worksheets[0];
            var result = 0;
            int i = 1;
            int s = 0;
            int count = 0;
            List<string> stockAccountList = new List<string>();
            StringBuilder sb = new StringBuilder();
            List<Company> list = new List<Company>();

            try
            {

                while (true)
                {
                    Cell celll = sheet.Cells[s, 0];
                    if (celll != null && !string.IsNullOrEmpty(celll.StringValue))
                    {
                        //1
                        string name = celll.StringValue;
                        //2
                        Cell cell1 = sheet.Cells[s, 1] != null ? sheet.Cells[s, 1] : null;
                        string cname = "";
                        if (cell1 != null && !string.IsNullOrEmpty(cell1.StringValue))
                            cname =  cell1.StringValue ;
                        var company = new Company()
                        {
                            Name=name+cname,
                            CreateTime = DateTime.Now
                        };
                        list.Add(company);
                        s++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {

            }
            ClothContext db = new ClothContext();
            var newList = new List<Company>();
            foreach (var company in list)
            {
                if (db.Companies.Where(o => o.Name == company.Name).Count() == 0)
                {
                    newList.Add(company);
                }
            }
            db.Companies.AddRange(newList);
            result = db.SaveChanges();
            return result;
        }

    }
}
